function GPM.UpdateColors( )
	local c = GPM.ConVars;
	local o = { };
	
	o.Local = Color(c["Local_r"]:GetInt(), c["Local_g"]:GetInt(), c["Local_b"]:GetInt(), c["Local_a"]:GetInt());
	o.Text = Color(c["Text_r"]:GetInt(), c["Text_g"]:GetInt(), c["Text_b"]:GetInt(), c["Text_a"]:GetInt());
	o.FriendText = Color(c["FriendText_r"]:GetInt(), c["FriendText_g"]:GetInt(), c["FriendText_b"]:GetInt(), c["FriendText_a"]:GetInt());
	o.ChatBG = Color(c["ChatBG_r"]:GetInt(), c["ChatBG_g"]:GetInt(), c["ChatBG_b"]:GetInt(), c["ChatBG_a"]:GetInt());
	o.HeaderText = Color(c["HeaderText_r"]:GetInt(), c["HeaderText_g"]:GetInt(), c["HeaderText_b"]:GetInt(), c["HeaderText_a"]:GetInt());
	o.HeaderBG = Color(c["HeaderBG_r"]:GetInt(), c["HeaderBG_g"]:GetInt(), c["HeaderBG_b"]:GetInt(), c["HeaderBG_a"]:GetInt());
	o.HeaderBGFlash = Color(c["HeaderBGFlash_r"]:GetInt(), c["HeaderBGFlash_g"]:GetInt(), c["HeaderBGFlash_b"]:GetInt(), c["HeaderBGFlash_a"]:GetInt());
	o.Outline = Color(c["Outline_r"]:GetInt(), c["Outline_g"]:GetInt(), c["Outline_b"]:GetInt(), c["Outline_a"]:GetInt());
	o.TextEntryBG = Color(c["TextEntryBG_r"]:GetInt(), c["TextEntryBG_g"]:GetInt(), c["TextEntryBG_b"]:GetInt(), c["TextEntryBG_a"]:GetInt());
	o.TextEntryCursor = Color(c["TextEntryCursor_r"]:GetInt(), c["TextEntryCursor_g"]:GetInt(), c["TextEntryCursor_b"]:GetInt(), c["TextEntryCursor_a"]:GetInt());
	o.TextEntryText = Color(c["TextEntryText_r"]:GetInt(), c["TextEntryText_g"]:GetInt(), c["TextEntryText_b"]:GetInt(), c["TextEntryText_a"]:GetInt());

	GPM.Colors = o;
end

GPM.Connections = { };

function GPM.NewPMWindow( to, ID )
	GPM.UpdateColors();

	local k = table.Count(GPM.Connections);
	local t = vgui.Create( "GPMFrame" );
	t:SetPos( 20, (ScrH()*0.5) - 22*k );
	t:SetSizeExpanded( 180, 200 );
	t:SetSizeMinimized( 180, 20 );
	t:SetPlayer( to );
	t:SetVisible( true );
	t:SetFlashDelay( 1 );
	
	t.ChatEntered = function( s, text )
		RunConsoleCommand( "SendPM", to:EntIndex(), text );
	end
	
	t.OnClose = function( s )
		s:Remove( );
		s = nil;
		GPM.Connections[ ID ] = nil;
	end
	
	GPM.Connections[ ID ] = t;
end

function GPM.IsSteamFriend( ply )
   return ply:GetFriendStatus() == "friend"
end

usermessage.Hook("IncommingPM", function(um)
	local from = um:ReadEntity();
	local ID = from:UniqueID();
	local m = um:ReadString( );
	
	if not GPM.Connections[ ID ] then
		GPM.NewPMWindow( from, ID );
	end
	
	GPM.Connections[ ID ]:AddText( m, from, true );
	GPM.Connections[ ID ]:StartFlash( );
end);

usermessage.Hook("NewMessage", function(um)
	local to = um:ReadEntity();
	local m = um:ReadString();
	local ID = to:UniqueID();
	
	if not GPM.Connections[ ID ] then
		GPM.NewPMWindow( to, ID );
	end
	
	GPM.Connections[ ID ]:AddText( m, LocalPlayer(), true );
end);

hook.Add("Think", "UpdateColors", function()
	if table.Count(GPM.Connections) > 0 then
		GPM.UpdateColors();
	end
end);